﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.Orders
{
    public partial class OrdersAuditing : BasePage
    {
        protected int OrderId = 0;
        private static IOrdersBLL ordersBLL = IocContainer.Instance.Resolve<IOrdersBLL>();
        protected ICustCashBLL CustCashBll = IocContainer.Instance.Resolve<ICustCashBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                    int.TryParse(Request.QueryString["orderId"], out OrderId);

                    if (OrderId > 0)
                    {
                        DataTable orderDetail;
                        Order ordeEntity = ordersBLL.GetOrderById(OrderId, out orderDetail);
                        this.txtOrderCode.Text = ordeEntity.OrderCode;
                        this.txtPrice.Text = ordeEntity.TotalPrice.ToString();
                        this.txtTaxPrice.Text = ordeEntity.TotalTaxPrice.ToString();
                        this.orderDetailBody.Text = BuildOrderDetail(orderDetail);

                        this.OrderProcessBody.Text = BuildOrderProcess(ordersBLL.GetOrderProcess(OrderId),OrderId,IndividualId);

                        CustCash cash = CustCashBll.GetCustomerCash(ordeEntity.CustId);
                        if (cash != null)
                        {
                            this.txtTotalMoney.Text = cash.TotalAmount.ToString();
                            this.txtCredit.Text = cash.CreditAmount.ToString();
                            this.txtCashAmount.Text = cash.CashAmount.ToString();
                        }
                    }
                
            }
        }

        protected string BuildOrderDetail(DataTable orderDetail)
        {
            StringBuilder result = new StringBuilder();
            if (orderDetail != null && orderDetail.Rows.Count > 0)
            {
                int i = 1;
                foreach (DataRow row in orderDetail.Rows)
                {
                    result.Append(i % 2 == 0 ? "<tr class='trbg'>" : "<tr>");
                    result.AppendFormat("<td>{0}</td>", row["FProductName"]);
                    result.AppendFormat("<td>{0}</td>", row["Fqty"]);
                    result.AppendFormat("<td>{0}</td>", row["Funit"]);
                    result.AppendFormat("<td>{0}</td>", row["Famount"]);
                    result.AppendFormat("<td>{0}</td>", row["Ftaxamount"]);
                    decimal storageNo = 0;
                    decimal.TryParse(row["storageNo"].ToString(), out storageNo);
                    result.AppendFormat("<td class='red'>{0}</td>", storageNo);
                    result.Append("</tr>");
                    i++;
                }
            }

            return result.ToString();
        }


        protected string BuildOrderProcess(DataTable orderProcess,int orderId,int empId)
        {
            StringBuilder result = new StringBuilder();
            if (orderProcess != null && orderProcess.Rows.Count > 0)
            {
                int i = 1;
                DataRow[] currentStep = orderProcess.Select("FEmpID=" +empId+ " and FBillInterID="+orderId+" and FStatus=1");
                int intCurrentStep = 1;
                if(currentStep!=null && currentStep.Length>0)
                {
                    intCurrentStep = Convert.ToInt32(currentStep[0]["FStep"].ToString());
                }
                foreach (DataRow row in orderProcess.Rows)
                {
                    result.Append(i % 2 == 0 ? "<tr class='trbg'>" : "<tr>");
                    result.AppendFormat("<td>{0}</td>", row["FName"]);
                    result.AppendFormat("<td>{0}</td>", row["FCheckDate"]);
                    result.AppendFormat("<td>{0}</td>", row["result"]);
                    result.AppendFormat("<td>{0}</td>", row["FNote"]);
                    int step =Convert.ToInt32(row["FStep"].ToString());
                    if (step < intCurrentStep)
                    {
                        result.Append("<td><input type='radio' value='" + step + "' name='b'/></td>");
                    }
                    else
                    {
                        result.Append("<td><input type='radio' value='" + step + "' name='b' disabled='disabled'/></td>");
                    }
                    result.Append("</tr>");
                    i++;
                }
            }

            return result.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int empId = IndividualId;
                int statue = 2;
                int bhStep = 1;
                int.TryParse(this.txtRejectStep.Value, out bhStep);
                int.TryParse(Request.QueryString["orderId"], out OrderId);

                if(this.rbYes.Checked)
                    statue = 2;
                else if(this.rbNo.Checked)
                    statue = 3;
                else if(this.rbReject.Checked)
                    statue = 4;
                ordersBLL.HandleOrder(empId, OrderId, statue, this.txtNotes.Text.Trim(), bhStep);

                ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.LoadMyToDoOrders();</script>");
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
